package com.itboye.jmsfhz.app.login.ui;

import java.util.Observable;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.cache.SharedPreferenceCache;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPContants;
import com.itboye.core.utils.SPUtils;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.logincontroller.LoginController;
import com.itboye.jmsfhz.app.logincontroller.LoginedState;
import com.itboye.jmsfhz.app.ui.main.MainPresenter;
import com.itboye.jmsfhz.data.model.AvatarBeans;
import com.itboye.jmsfhz.data.model.AvatarDataBeans;
import com.itboye.jmsfhz.data.model.PersonDataBean;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 2016.5.13
 * 
 * @author liu
 * 
 */
@SuppressLint("ResourceAsColor")
public class LoginActivity extends BaseActivity implements OnClickListener,
		Observer {

	ImageView bindmobil_logo, wixin_login_bt;
	TextView login_text, forget_pass_bt;
	EditText login_input_phonenum, login_input_password;
	Button login_now;
	private String phone;
	private String password;
	MainPresenter mainPresenter;
	TextView loginByMessage;
	Button login_login_resgister;// 原appSecret:252d40ea15fa4266e1e6854723a1df63-----现在d7fead25487e15bbbd906659bac5a689
	public static String LOGINEXIT = "com.itboye.sunsun.exit";

	private ImageView imgok, imgStudent;
	private TextView txtQiYe, tvStudent;

	// 学生
	private String StuDent = "StudentUpdate";
	private int role;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_login;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onMyCreate() {
		mainPresenter = new MainPresenter(this);
//		IntentFilter filter1 = new IntentFilter(LOGINEXIT);
//		registerReceiver(emailBroadcastReceiver, filter1);

		imgok.setBackgroundResource(R.drawable.ok);
		imgStudent.setBackgroundResource(R.drawable.no);
		role = 6;
		txtQiYe.setTextColor(getResources().getColor(R.color.main_color));
		tvStudent.setTextColor(getResources().getColor(R.color.gray));
		Intent qiye0 = new Intent("qiye");
		sendBroadcast(qiye0);

	}

//	BroadcastReceiver emailBroadcastReceiver = new BroadcastReceiver() {
//		public void onReceive(Context context, Intent intent) {
//			LoginActivity.this.finish();
//
//		};
//	};

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.login_now:

			onLogin();
			break;
		case R.id.forget_pass_bt:
//			Intent intent1 = new Intent(this, ForgetPasswordActivity.class);
//			startActivity(intent1);
			break;
		case R.id.login_login_resgister:
			Intent intent = new Intent(this, RegiterActivity.class);
			startActivity(intent);
			break;
		case R.id.txtQiYe:
			imgok.setBackgroundResource(R.drawable.ok);
			imgStudent.setBackgroundResource(R.drawable.no);
			txtQiYe.setTextColor(getResources().getColor(R.color.main_color));
			tvStudent.setTextColor(getResources().getColor(R.color.gray));
			role = 6;
			Intent qiye1 = new Intent("qiye");
			sendBroadcast(qiye1);
			break;
		case R.id.imgok:
			imgok.setBackgroundResource(R.drawable.ok);
			imgStudent.setBackgroundResource(R.drawable.no);
			txtQiYe.setTextColor(getResources().getColor(R.color.main_color));
			tvStudent.setTextColor(getResources().getColor(R.color.gray));
			role = 6;
			Intent qiye = new Intent("qiye");
			sendBroadcast(qiye);
			break;
		case R.id.tvStudent:
//			IntentFilter student = new IntentFilter(StuDent);
//			registerReceiver(emailBroadcastReceiver, student);
			imgok.setBackgroundResource(R.drawable.no);
			imgStudent.setBackgroundResource(R.drawable.ok);
			txtQiYe.setTextColor(getResources().getColor(R.color.gray));
			tvStudent.setTextColor(getResources().getColor(R.color.main_color));
			role = 5;
			Intent intent2 = new Intent("studengt");
			sendBroadcast(intent2);
			break;
		case R.id.imgStudent:
			imgok.setBackgroundResource(R.drawable.no);
			imgStudent.setBackgroundResource(R.drawable.ok);
			txtQiYe.setTextColor(getResources().getColor(R.color.gray));
			tvStudent.setTextColor(getResources().getColor(R.color.main_color));
			role = 5;
			
			Intent intent3 = new Intent("studengt");
			sendBroadcast(intent3);
			break;
		default:
			break;
		}
	}

	public void onLogin() {
		phone = login_input_phonenum.getText().toString();
		password = login_input_password.getText().toString();
		// 检查是否是手机号号
		Pattern p = Pattern
				.compile("^((13[0-9])|(17\\d)|(14\\d)|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(phone);
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
		} else if (TextUtils.isEmpty(password)) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
		} else {
			SharedPreferenceCache.set(SPContants.USERNAME, phone, 1);
			SharedPreferenceCache.set(SPContants.PASSWORD, password, 1);

			mainPresenter.login(phone, password, role + "");
			login_now.setText("正在登录...");

		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		login_now.setText("正在登录...");
		ResultEntity entity = super.handlerError(arg1);
		if (entity != null) {
			Log.d("MainActivity", entity.getEventType());
			Log.d("MainActivity", entity.getMsg());
			Log.d("MainActivity", entity.getCode());
			Log.d("MainActivity", entity.getData().getClass().getName());

			if (entity.getEventType().equals(MainPresenter.LoginSuccessEvent)) {
				whenLoginSuccess(entity);
			} else if (entity.getEventType().equals(
					MainPresenter.RegisterSuccessEvent)) {
				whenRegisterSuccess(entity);
			} else {
				login_now.setText("登陆");
			}
		}

		Log.d("MainActivity", "update 完成");
	}

	public void whenRegisterSuccess(ResultEntity result) {
		System.out.print("result.getData" + result.getData());
	}

	public void whenLoginSuccess(ResultEntity result) {
		LoginController.setLoginState(new LoginedState());
		PersonDataBean model = (PersonDataBean) result.getData();
		SPUtils.put(this, null, SpPublicParams.IS_LOGINED, true);
		SPUtils.put(this, null, SpPublicParams.UID, model.getMember_model()
				.getUid());
		SPUtils.put(this, null, SpPublicParams.USER_NAME, model.getUsername());
		SPUtils.put(this, null, SpPublicParams.NICK_NAME, model
				.getMember_model().getNickname());
		SPUtils.put(this, null, SpPublicParams.URL, model.getMember_model()
				.getHead());
		SPUtils.put(this, null, SpPublicParams.EMAIL, model.getEmail());
		SPUtils.put(this, null, SpPublicParams.UnsePaswold, model.getPassword());
		SPUtils.put(this, null, SpPublicParams.RENZHENG, model
				.getMember_model().getIs_auth_e());
		SPUtils.put(this, null, SpPublicParams.ROLE, model
				.getAuth_group_model_list().get(0).getId());
		String	 time = String.valueOf((int) (Math.random() * 10));
		SPUtils.put(this, null, SpPublicParams.URL, GlobalConfig.HEAD+model.getMember_model().getUid()+"&&random"+time);
		SPUtils.put(this, null, SpPublicParams.IMAGE_URL, GlobalConfig.SHENFEN+model.getMember_model().getUid()+"&&random"+time);
		//
		ByAlert.alert("登录成功");
		setResult(102);
		// 发送广播通知更改头像
		Intent intent2 = new Intent("changeavart");
		sendBroadcast(intent2);

		Intent intent8 = new Intent();
		setResult(1024, intent8);
		finish();
	}
}
